package org.example.leetcode;

import java.util.Stack;

/**
 * @version 1.0.0
 * @author: lynn
 * @description:
 * @date: 2021/7/27 10:49
 */
public class LC24 {
    public static void main(String[] args) {

    }
    //k个节点翻转
    public ListNode swapPairs(ListNode head) {
        if (head==null || head.next==null) return head;
        int k=2;int localK=k;
        ListNode curNode=head;
        Stack<ListNode> stack=new Stack<>();
        while(curNode!=null && localK>0){
            stack.push(curNode);
            curNode=curNode.next;
            localK--;
        }
        if (stack.size()<k){
            return head; //fixme 肯定是没问题的，类比第一次，还没递归的时候
        }
        ListNode dummyNode=new ListNode(0);
        ListNode tail=dummyNode;
        while (stack!=null){
            tail.next=stack.pop(); //fixme tail.next 可以返回dummyNode.next
            tail=tail.next;
        }
        tail.next=swapPairs(curNode);
        return dummyNode.next;
    }
    private class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }
}
